import WechatJSBridge from '@/utils/wechatJSBridge'
import {
  getConnect
} from '@/utils/auth'

const state = {
  connect: getConnect(),
  data: null
}

const mutations = {
  SET_SHARE_DATA: (state, data) => {
    state.data = data
    console.log('SET_SHARE_DATA ing...')
    const sdkBridge = new WechatJSBridge(data.link)
    sdkBridge.updateAppMessageShareData(data).then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
  }
}

const actions = {
  // 设置分享数据
  setData ({
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      if (state.connect) {
        commit('SET_SHARE_DATA', data)
        resolve(data)
      } else {
        reject(data)
      }
    })
  },
  changeData ({
    commit
  }, data) {
    commit('SET_SHARE_DATA', data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
